package com.lemzdo.mail.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lemzdo.mail.product.dao.BrandDao;
import com.lemzdo.mail.product.dao.CategoryDao;
import com.lemzdo.mail.product.entity.BrandEntity;
import com.lemzdo.mail.product.entity.CategoryEntity;
import com.lemzdo.mail.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemzdo.common.utils.PageUtils;
import com.lemzdo.common.utils.Query;

import com.lemzdo.mail.product.dao.CategoryBrandRelationDao;
import com.lemzdo.mail.product.entity.CategoryBrandRelationEntity;
import com.lemzdo.mail.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *  细节化save方法 将brandId和catelogId 对应的名字查询出来并存入关系表中
     * @param categoryBrandRelation 关系表实体字段
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //拿到brandId和catelogId
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //查询详细的品牌名字和分类名字(需要注入品牌dao和分类dao)
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        //将名字设置到categoryBrandRelation中
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        //直接调用自己的save方法 将包含名字的信息保存好
        this.save(categoryBrandRelation);
    }

    /**
     *  关联更新 通过更新品牌的时候 更新关系表中的冗余数据（name）
     * @param brandId 品牌id
     * @param name 品牌名字
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();

        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);

        //根据brand的id修改brand的name
        this.update(categoryBrandRelationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    /**
     * 获取分类关联的品牌
     * @param catId 分类id
     * @return 自定义idRespVo 封装了 brandId brandName
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = this.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        //为了避免其他地方调用这个方法 所以还是封装完整品牌信息 并返回
        List<BrandEntity> brandEntities = relationEntities.stream().map(item -> {
            Long brandId = item.getBrandId();
            return brandService.getById(brandId);
        }).collect(Collectors.toList());

        return brandEntities;
    }
}